﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using XCBPM.Mobile.Common;

namespace XCBPM.Mobile.DAL
{
    public class ProcessTasksDAO
    {
        /// <summary>
        /// 待办
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="processName"></param>
        /// <param name="PageNo"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public DataTable GetMyToDoJob(string userName, string processName, int PageNo, int PageSize)
        {
            //status = 1 当前需要处理的
            string sql = @"select * from 
                            (select *,ROW_NUMBER() over(order by INCIDENT asc) as rowId
                            from TASKS where 1=1 and  STATUS = 1 {0}) temp where temp.rowId between {1} and {2}";

            string condition = string.Empty;

            if (string.IsNullOrEmpty(userName))
                return null;

            if (!string.IsNullOrEmpty(userName))
            {
                condition += " and ASSIGNEDTOUSER = '" + userName + "'"; ;
            }

            if (!string.IsNullOrEmpty(processName))
            {
                condition += " and PROCESSNAME = '" + processName + "'";
            }

            if (PageNo == 0 || PageSize == 0)
                return null;

            int pageLower = (PageNo - 1) * PageSize + 1;
            int pageHiger = PageNo * PageSize;

            sql = string.Format(sql, condition, pageLower, pageHiger);

            DataSet ds = DBHelper.Query(sql);

            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                return dt;
            }
            else
                return null;
        }

        /// <summary>
        /// 已审批
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="processName"></param>
        /// <param name="PageNo"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public DataTable AlReadyApprovalJs(string userName, string processName, int PageNo, int PageSize)
        {
            string sql = @"select * from 
                            (select *,ROW_NUMBER() over(order by INCIDENT asc) as rowId
                            from TASKS where 1=1 and  STATUS = 3 {0}) temp where temp.rowId between {1} and {2}";

            string condition = string.Empty;

            if (string.IsNullOrEmpty(userName))
                return null;

            if (!string.IsNullOrEmpty(userName))
            {
                condition += " and ASSIGNEDTOUSER = '" + userName + "'"; ;
            }

            if (!string.IsNullOrEmpty(processName))
            {
                condition += " and PROCESSNAME = '" + processName + "'";
            }

            if (PageNo == 0 || PageSize == 0)
                return null;

            int pageLower = (PageNo - 1) * PageSize + 1;
            int pageHiger = PageNo * PageSize;

            sql = string.Format(sql, condition, pageLower, pageHiger);

            DataSet ds = DBHelper.Query(sql);

            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                return dt;
            }
            else
                return null;
        }

        /// <summary>
        /// 我发起的
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="processName"></param>
        /// <param name="PageNo"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public DataTable MyStartFlowJs(string userName, string processName, int PageNo, int PageSize)
        {
            string sql = @"select * from 
                            (select *,ROW_NUMBER() over(order by INCIDENT asc) as rowId
                            from TASKS where 1=1 and STEPLABEL = '填写申请单' {0}) temp where temp.rowId between {1} and {2}";

            string condition = string.Empty;

            if (string.IsNullOrEmpty(userName))
                return null;

            if (!string.IsNullOrEmpty(userName))
            {
                condition += " and ASSIGNEDTOUSER = '" + userName + "'";
            }

            if (!string.IsNullOrEmpty(processName))
            {
                condition += " and PROCESSNAME = '" + processName + "'";
            }

            if (PageNo == 0 || PageSize == 0)
                return null;

            int pageLower = (PageNo - 1) * PageSize + 1;
            int pageHiger = PageNo * PageSize;

            sql = string.Format(sql, condition, pageLower, pageHiger);

            DataSet ds = DBHelper.Query(sql);

            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                return dt;
            }
            else
                return null;
        }
    }
}
